package com.han.filters;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Professor;
import com.han.bom.Student;
import com.han.bom.User;
import com.han.dao.UserDAO;

/**
 * Servlet Filter implementation class LoginFilter
 */
@WebFilter({ "/Professor/*", "/Student/*" })
public class LoginFilter implements Filter {

	private static Logger logger = Logger.getLogger(LoginFilter.class.getName());
	
    /**
     * Default constructor. 
     */
    public LoginFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		
		UserDAO userdao = new UserDAO();
		String username = ((HttpServletRequest) request).getRemoteUser();
		HttpSession session = ((HttpServletRequest) request).getSession();
		
		if(session.getAttribute("student")==null && session.getAttribute("professor")==null){
		
			User user=null;
			try {
				user = userdao.retrieveUserByUsername(username);
			} catch (ClassNotFoundException e1) {
				logger.error(e1.getMessage(), e1);
				e1.printStackTrace();
			} catch (SQLException e1) {
				logger.error(e1.getMessage(), e1);
				e1.printStackTrace();
			}
			
			if(user instanceof Student){
				
				Student student = (Student)user;
								
				session.setAttribute("student", student);
				
				String uri = ((HttpServletRequest) request).getRequestURI();
				
				if(!"/BetaTestEngine/Student/Home.jsp".equals(uri)){
					((HttpServletResponse) response).sendRedirect("/BetaTestEngine/Student/Home.jsp");
				}
				
			}else{
				
				Professor professor = (Professor)user;
				
				professor.setStudents(new ArrayList<Student>());
				
				session.setAttribute("professor", professor);
				
				String uri = ((HttpServletRequest) request).getRequestURI();
				
				if(!"/BetaTestEngine/Professor/Home.jsp".equals(uri)){
					((HttpServletResponse) response).sendRedirect("/BetaTestEngine/Professor/Home.jsp");
				}
				
			}
		}
		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
